﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;

public partial class _controls_AddWorkItem : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
         
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        bool hasImage = this.uplImage.PostedFile.ContentLength != 0;

        WorkItem ach = IMadeThisService.Current.AddAchievment(this.txtTitle.Text, this.txtDescription.Text, this.txtUrl.Text, this.ucDateTime.SelectedMonth , this.ucDateTime.SelectedYear, hasImage);
        
        if(ach == null)
        {
            // TODO: Ach. not correctly saved.
        }

        if (this.uplImage.PostedFile.ContentLength != 0)
        {
            string imagePath = FileManager.Save(this.uplImage.PostedFile);
            
            FileStream stream = new FileStream(imagePath, FileMode.Open);
            //ImageManager.ResizeImageAndUpload(stream, Server.MapPath("~/_matser/"), "hello.jpg", 200, 400);
            //ImageManager.ResizeFromStream(Server.MapPath("~/_master/") + "hello.jpg", 130, stream);
            ImageManager.ResizeImage(stream, Server.MapPath("~/_media/small/"), ach.ID.ToString() , ImageEncoding.Jpeg, 130);
            ImageManager.ResizeImage(stream, Server.MapPath("~/_media/large/"), ach.ID.ToString(), ImageEncoding.Jpeg, 400);

            stream.Close();

            File.Delete(imagePath);
            
            hasImage = true;
        }
        
        Response.Redirect("index.aspx");
    }


}
